﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Filter;
using Cofe.Core.Implements;
using Cofe.Core.Property;

namespace Cofe.Data
{
    public static class IRepositoryExtension
    {
        public static T GetValue<T>(this IRepository repository, DatabaseEntry dbEntry, object property)
        {
            var exp = CofeServices.PropertyDictionary.GetPropertyExpression<T>(property);
            return repository.GetValue(dbEntry, exp);
        }

        //public static void AddOrUpdate(this IRepository manager, params ICofeSystemInfo[] entries)
        //{
        //    manager.AddOrUpdate((from e in entries select e.Properties).ToArray());
        //}

        //public static DatabaseEntry Find(this IRepository manager, string parsePath)
        //{
        //    return manager.FindFirst(e => e.ParseName == parsePath);
        //}


        //public static bool Exists(this IRepository manager, string parsePath)
        //{
        //    return manager.Exists(e => e.ParseName == parsePath);
        //}

        //public static DatabaseEntry FindFirst(this IRepository manager, 
        //    Expression<Func<CofeSystemInfo, bool>> query)
        //{
        //    return manager.FindAll(query).FirstOrDefault();
        //}

        //public static IEnumerable<DatabaseEntry> FindAll(this IRepository manager, IEntryFilter[] entryFilters, params IOptionFilter[] optionFilters)
        //{
        //    var expression =  manager.EntryFilterExpressionFactory.ConstructExpression(FilterCollections.FromFilters(entryFilters, optionFilters));
        //    return manager.FindAll(expression);
        //}

        //public static IEnumerable<DatabaseEntry> FindAll(this IRepository manager, IEntryFilter entryFilter, params IOptionFilter[] optionFilters)
        //{
        //    return manager.FindAll(new IEntryFilter[] { entryFilter }, optionFilters);
        //}

        //public static IEnumerable<T> FindAll<T>(this IRepository manager, IEntryFilter[] entryFilters, params IOptionFilter[] optionFilters) where T : ICofeSystemInfo
        //{
        //    foreach (var dbEntry in manager.FindAll(entryFilters, optionFilters))
        //    {
        //        if (dbEntry.Implementations.Any(i => i is T))
        //            yield return (T)(object)dbEntry.Implementations.First(i => i is T);
        //    }
        //}

        //public static IEnumerable<T> FindAll<T>(this IRepository manager, IEntryFilter entryFilter, params IOptionFilter[] optionFilters) where T : ICofeSystemInfo
        //{
        //    return manager.FindAll<T>(new IEntryFilter[] { entryFilter }, optionFilters);
        //}

        //public static T Find<T>(this IRepository manager, string parsePath) where T : ICofeSystemInfo
        //{
        //    return manager.FindFirst<T>(e => e.ParseName == parsePath);
        //}

        //public static T FindFirst<T>(this IRepository manager,
        //    Expression<Func<CofeSystemInfo, bool>> query) where T : ICofeSystemInfo
        //{
        //    return manager.FindAll<T>(query).FirstOrDefault();
        //}
    }
}
